/*
 * @Author: wuwk
 * @Date: 2021-12-21 09:45:02
 * @LastEditors: wuwk
 * @LastEditTime: 2021-12-22 09:37:23
 * @Description: 
 */

import numeral from 'numeral';
// 格式化数字
/**
 * @type %需要转为%
 * @type 非0 如果小于一万不加0
 * @unit 单位 如果有传单位，则直接按传入的单位转换
 */
export const numConversion = function (params) {
  const {
    numString,
    type,
    unit
  } = params;
  const notNumber = isNaN(+numString);
  const numberArr = (numString + '').split('.');
  const foramtMap = {
    "万": 4,
    "亿": 8,
    "万亿": 12,
  }
  let fmt = '0,0.00';
  let _unit = unit;
  let suffixNum = '';
  let beDivided = numeral(numString);
  if (notNumber || !numberArr || !numberArr.length) {
    console.log('数据异常', numString);
    return {
      value: numString == null || numString == 'null' ? 0 : numString,
      unit: _unit
    };
  }
  if (type === '%') {
    // 处理百分比
    const oldValue = beDivided;
    return {
      value: beDivided.multiply(100).format('0.00'),
      originalNum: oldValue.value(),
      unit: '%'
    };
  }
  var divisorLength = (numberArr[0] + '').length;
  if (_unit && foramtMap[_unit]) {
    suffixNum = foramtMap[_unit];
  } else if (divisorLength >= 5 && divisorLength < 9) {
    suffixNum = 4;
    _unit = '万';
  } else if (divisorLength >= 9 && divisorLength < 13) {
    suffixNum = 8;
    _unit = '亿';
  } else if (divisorLength >= 13) {
    suffixNum = 12;
    _unit = '万亿';
  } else {
    if (type !== 0 && beDivided.value() < 10000 && numberArr.length < 2) fmt = '0,0';
  }
  beDivided = beDivided.divide(10 ** suffixNum);
  return {
    value: beDivided.format(fmt),
    originalNum: beDivided.value(),
    unit: _unit,
  };
};

/**
 * 格式化坐标轴数值
 * @param {Number} value 
 * @returns 
 */
export const formatterAxisNum = function (value) {
  if (value >= 10000 && value < 10000000) {
    value = value / 10000 + "万";
  } else if (value >= 10000000) {
    value = value / 10000000 + "千万";
  }
  return value;
}